package com.smart.common.config.knife4j;


import com.smart.common.entities.constant.SmartConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.List;


/**
 * @author Administrator
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {


    /**
     * API全局参数配置工具类
     */
    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder().title("国牧").description("国牧数据接口").version("1.0").build();
    }


    private Parameter getHeaderPatter(String name, String description) {
        ModelRef modelRef = new ModelRef("String");
        ParameterBuilder config = new ParameterBuilder();
        return config.name(name).description(description).modelRef(modelRef).parameterType("header").required(true).build();


    }

    /**
     * Api请求参数配置类 工具类
     */
    public List<Parameter> commConfig(String pkg) {
        Parameter token = getHeaderPatter(SmartConstant.TOKEN_NAME, "Bearer+空格+登录时的AccessToken ");
        Parameter client = getHeaderPatter("x-api-client", "请求终端");
        Parameter nonce = getHeaderPatter("x-api-nonce", "随机字符串");
        Parameter imei = getHeaderPatter("x-api-imei", "设备串号");
        Parameter timestamp = getHeaderPatter("x-api-timestamp", "秒级时间戳");
        Parameter sign = getHeaderPatter("x-api-sign", "请求签名 ,算法：大写32位MD5(SMART-客户端-随机字符串-设备串号-时间戳)");

      String checkPkgName  ="com.smart.".concat(SmartConstant.CHECK_HEADER_PKG).concat(".app") ;
        if (checkPkgName.equals(pkg)) {
            return List.of( client, nonce, imei, timestamp, sign,token);
        }
        //只注入身份签名
        return List.of(token);

    }

    /**
     * 模块注册工具类
     */
    private Docket createApi(String groupName, String packagePath) {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(webApiInfo())
                .groupName(groupName)
                .select().apis(RequestHandlerSelectors.basePackage(packagePath))
                .paths(PathSelectors.any()).build()
                .useDefaultResponseMessages(false)
                .globalOperationParameters(commConfig(packagePath));

    }

    @Bean(value = "wapDoc")
    public Docket wapDoc() {
        return createApi("移动端-API文档", "com.smart.wap.app");
    }

    @Bean(value = "wapH5")
    public Docket wapH5() {
        return createApi("公众号-H5", "com.smart.wap.h5");
    }

    @Bean(value = "workDoc")
    public Docket workDoc() {
        return createApi("WTB-喂养端-API文档", "com.smart.wap.work");
    }

    @Bean(value = "merchantDoc")
    public Docket merchantDoc() {
        return createApi("WTB-商户端-API文档", "com.smart.wap.merchant");
    }


    @Bean(value = "otherDoc")
    public Docket otherDoc() {
        return createApi("公开回调系统", "com.smart.other");
    }

    @Bean(value = "managerDoc")
    public Docket managerDoc() {
        return createApi("后端接口-API文档", "com.smart.manager.pop.controller");
    }

}

